<?php

namespace App\Http\Controllers\Mobile\Personal;
use App\Models\Common\CollArea;
use App\Models\Personal\Resume;
use App\Models\Personal\ResumeSend;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Style;

class ResumeSendController extends CommonController{

    /**
     * 简历投递记录
     * @param Request $request
     * @return mixed
     */
    public function resumeSend(Request $request){
        $user = auth()->guard('personal')->user();
        $resume = Resume::where('default', 1)->where('uid', $user->uid)->first(['id']);
        if(empty($resume)){
            return redirect('mobile/personal/resume_create');
        }
        //关键词搜索
        $keyword  = trim($request->keyword);
        $job_status  =$request->job_status;
        //简历投递记录
        $resume_send = DB::table('resume_send as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_job as t3', 't1.job_id', '=', 't3.id')
            ->select(
                't1.created_at','t1.id','t1.status','t1.com_id','t1.job_id',
                't2.company_name',
                't3.job_title','t3.location1'
            )
            ->where('t1.personal_uid',$user->uid)
            ->where(function ($query) use($job_status) {
                if($job_status !=''){
                    $query->where('t1.status', $job_status);
                }
            })
            ->where('t1.status','!=',4)
            ->where('t1.resume_id', $resume->id)

            ->where(function ($query) use($keyword) {
                if(strlen($keyword) > 0) {
                    $query->where('t3.job_title', 'like', '%'.$keyword.'%')
                        ->orWhere('t2.company_name','like','%'.$keyword.'%');
                }
            })
            ->orderBy('updated_at', 'desc')
            ->paginate(10);
        //处理数组
        foreach($resume_send as $v){
            $v->location1  = ($v->location1) ?  CollArea::getAreaName($v->location1) : '';
            $v->company_name = str_limit($v->company_name,24);
            $v->job_title = str_limit($v->job_title,24);
            $v->created_at = substr($v->created_at,0,16);
            switch($v->status){
                case 0 : $v->statusval = '未查看'; break;
                case 1 : $v->statusval = 'HR已查看'; break;
                case 2 : $v->statusval = '同意面试'; break;
                case 3 : $v->statusval = '已被婉拒'; break;
                case 4 : $v->statusval = '删除';
            }
        }
        //分页分配参数 过滤空值
        $appends =  arrayFilter( array(
            'keyword' => $keyword,
            'job_status' =>  $job_status
        ));
        return Style::view('mobile.personal.resumeSend',compact('resume_send','job_status','appends'));
    }

    /**
     * 投递记录删除
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postDel(Request $request){
        $user = auth()->guard('personal')->user();
        $input = $request->only('id');
        $input['status'] = 4;
        if(!empty($request->id)){
            $result = ResumeSend::where('personal_uid', $user->uid)->where('id', $request->id)->update($input);
            if($result){
                return response()->json(['status' => 200,'message'=>"删除投递记录成功"]);
            }else{
                return response()->json(['status' => 404,'message'=>"删除投递记录失败"]);
            }
        }else{
            return response()->json(['status' => 404,'message'=>"删除投递记录失败"]);
        }
    }
}